﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace NCAABasketballTournament
{
    public partial class MainPage : UserControl
    {
        List<Team> oTeams = null;
        Tournament oTournament = null;

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ReadFromResource(@"NCAABasketballTournament.NCAA.csv");
        }



        private void CreateBracketDisplay()
        {
            oTournament = new Tournament();
            oTournament.RunTournament(oTeams);

            var oPage = new BracketPage() { DataContext = oTournament };
            Display.Children.Add(oPage);

            var oControl = new BracketDisplay() { DataContext = oTournament.MidWest };
            Display.Children.Add(oControl);

            oControl = new BracketDisplay() { DataContext = oTournament.East  };
            Display.Children.Add(oControl);

            oControl = new BracketDisplay() { DataContext = oTournament.West   };
            Display.Children.Add(oControl);

            oControl = new BracketDisplay() { DataContext = oTournament.South };
            Display.Children.Add(oControl);

            oControl = new BracketDisplay() { DataContext = oTournament.Finals };
            Display.Children.Add(oControl);


       }
        private void Bracket_Click(object sender, RoutedEventArgs e)
        {
            PopupBracket oPage = new PopupBracket();
            oPage.Content = new BracketPage();
            oPage.Show();
        }

        private void ReadFromResource(string sStreamName)
        {
           oTeams = new List<Team>();

            using (System.IO.Stream oStream = this.GetType().Assembly.GetManifestResourceStream(sStreamName))
            {
                try
                {
                    StreamReader inStream = new System.IO.StreamReader(oStream);
                    while (inStream.EndOfStream == false)
                    {
                        string sResult = inStream.ReadLine();
                        if (sResult.StartsWith("Team,"))
                            continue;

                        string[] sItems = sResult.Split(',');
                        if (string.IsNullOrEmpty(sItems[0]))
                            continue;

                        var oTeam = new Team()
                        {
                            TeamName = sItems[0],
                            TotalRebounds = double.Parse(sItems[1]),
                            PersonalFouls = double.Parse(sItems[2]),
                            Region = sItems[3],
                            Seed = int.Parse(sItems[4]),
                            RPI = double.Parse(sItems[5])
                        };

                        oTeams.Add(oTeam);
                    }
                    CreateBracketDisplay();            
                }
                catch (Exception oEx)
                {
                    MessageBox.Show(oEx.Message);
                }
            }

        }


        private void Open_Click(object sender, RoutedEventArgs e)
        {
            ReadFromResource(@"NCAABasketballTournament.NCAA.csv");
            
            //OpenDialog();
        }

        private void OpenDialog()
        {
            // -- User chooses the file
            OpenFileDialog myDialog = new OpenFileDialog();
            myDialog.Filter = "csv Files (*.csv)|*.csv";
            myDialog.ShowDialog();

            oTeams = new List<Team>();

            // -- File read and parsed
            StreamReader inStream = myDialog.File.OpenText();
            while (inStream.EndOfStream == false)
            {
                string sResult = inStream.ReadLine();
                if (sResult.StartsWith("Team,"))
                    continue;

                string[] sItems = sResult.Split(',');
                if ( string.IsNullOrEmpty(sItems[0]) )
                    continue;

                var oTeam = new Team()
                {
                    TeamName = sItems[0],
                    TotalRebounds = double.Parse( sItems[1]),
                    PersonalFouls = double.Parse(sItems[2]),
                    Region = sItems[3],
                    Seed = int.Parse( sItems[4] ),
                    RPI = double.Parse(sItems[5])
                };

                 
                 oTeams.Add(oTeam);
            }            
             
            //MyGrid.ItemsSource = oTeams;
            CreateBracketDisplay();            
        }


    }
}
